"""
override 重写 / 重载 / 覆盖
"""
import scrapy
from scrapy import Selector, Request
from scrapy.http import HtmlResponse

from spider2107.items import MovieItem


class DoubanSpider(scrapy.Spider):
    name = "douban"
    allowed_domains = ["movie.douban.com"]

    # socks5代理
    def start_requests(self):
        for page in range(10):
            yield Request(
                url=f'https://movie.douban.com/top250?start={page * 25}&filter=',
                #  添加代理
                # meta={'proxy': 'http://127.0.0.1:1080'}
            )

    # 解析数据的函数
    def parse(self, response: HtmlResponse, **kwargs):
        sel = Selector(response)
        # 获取当前页面的所有电影信息
        list_items = sel.css('#content > div > div.article > ol > li')
        for list_item in list_items:
            movie_item = MovieItem()
            # 获取电影名称
            movie_item['title'] = list_item.css('span.title::text').extract_first()
            # 获取电影评分
            movie_item['rank'] = list_item.css('span.rating_num::text').extract_first()
            # 获取电影简介
            movie_item['subject'] = list_item.css('span.inq::text').extract_first()
            # yield作用: 将数据交给管道处理 相当于continue和return的结合体
            yield movie_item
